package sysconfig

import (
	"encoding/json"
	"fmt"
	"github.com/jinzhu/configor"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"

)

var Config = struct {
	APPName string `default:"app name"`
	common.App_Config_Data `anonymous:"true"`

	LbsQq struct{
		Key   string
	}

/*

	Debug struct {
		IsDebugMode bool `default:false env:"IsDebugMode"`
	}

	Database struct {
		Master_Db string `required:"true"`
	}

	Http struct {
		Port   int    `required:"true"`
		Domain string `required:"true"`
	}

	Session struct {
		If_Redis_session bool `default:false`
	}

	Redis struct {
		Redis_Host string `required:"true"`
		Redis_Port string `required:"true"`
		Redis_Pswd string
	}

	Aliyun_Oss struct {
		Key_Id            string
		Key_Secret        string
		Endpoint          string
		Endpoint_Internal string
		Bucket            string
	}

	Code struct {
		Prjname         string `required:"true"`
		Prjprex         bool   `default:false`
		Req_SignKey     string `required:"true"`
		Token_SecretKey string `required:"true"`
	}

	Wechat struct {
		// 小程序
		WX_APPID     string
		WX_APPSECRET string

		// 公众号
		WX_MP_APPID     string
		WX_MP_APPSECRET string

		WX_MERCHANT_ID string
		WX_PAY_KEY     string

		WX_PAYMENT_CERT string
		WX_PAYMENT_KEY  string
		WX_PAYMENT_ROOT string
		WX_PAY_NOTIFY_URL string
	}

	LbsQq struct{
		Key   string
	}
*/

}{}

/*DB struct {
	Name     string
	User     string `default:"root"`
	Password string `required:"true" env:"DBPassword"`
	Port     uint   `default:"3306"`
}*/

/*Contacts []struct {
	Name  string
	Email string `required:"true"`
}*/

func init() {
	isExist, err := utils.PathExists("appconfig.yml")
	if !isExist {
		panic("appconfig.yml not found:")
	}

	err = configor.Load(&Config, "appconfig.yml")
	if err != nil {
		panic(err)
	}

	configJson, _ := json.MarshalIndent(&Config, "", "    ")
	fmt.Printf("load config: %s\n", configJson)

	if Config.Wechat.WX_PAY_NOTIFY_URL == "" {
		fmt.Print("WARN: WX_PAY_NOTIFY_URL is empty")
		panic("WARN: WX_PAY_NOTIFY_URL is empty")
	}
}

func SecretKey() string {
	return Config.Code.Token_SecretKey
}

/*func TokenPrefix() string {
	prefix := config.GetIniByKey("CODE", "PRJ_TOKEN_PREFIX")
	return prefix
}*/

